<?php

/**
 * @package Framework03
 * @author Walter Tamboer
 */

/**
 * The cookie jar loads and holds cookies.
 */
class CookieJar
{
	/**
	 * Gets a cookie by it's name.
	 * @param string $Name The name of the cookie to get.
	 * @return Cookie Returns the requested cookie.
	 */
	public static function Get($Name)
	{
		if (!isset($_COOKIE[$Name]))
			return null;
			
		$Value = $_COOKIE[$Name];

		if (get_magic_quotes_gpc())
			$Value = stripslashes($Value);

		$Cookie = new Cookie;
		$Cookie->SetName($Name);
		$Cookie->SetData(unserialize($Value));
		$Cookie->SetLife(0);

		return $Cookie;
	}
	/**
	 * Saves a cookie as the given name.
	 * @param string $Name The name of the cookie to set.
	 * @param mixed $Vakue The value of the cookie.
	 * @param int $Life The amount of time in seconds this cookie will exist.
	 * @param string $Path The path on the server in which the cookie will be available on.
	 * @param string $Domain The domain that the cookie is available on.
	 * @param bool $Secure Indicates whether or not the cookie should only be transmitted over a secure HTTPS connection from the client.
	 * @param bool $HttpOnly When TRUE the cookie will be made accessible only through the HTTP protocol.
	 */
	public static function Set($Name, $Value, $Life = 3600, $Path = '/', $Domain = null, $Secure = false, $HttpOnly = false)
	{
		setcookie($Name, serialize($Value), time() + $Life, $Path, $Domain, $Secure, $HttpOnly);
	}
	/**
	 * Deletes the cookie with the given name.
	 * @param string $Name The name of the cookie to set.
	 * @param string $Path The path on the server in which the cookie will be available on.
	 * @param string $Domain The domain that the cookie is available on.
	 * @param bool $Secure Indicates whether or not the cookie should only be transmitted over a secure HTTPS connection from the client.
	 * @param bool $HttpOnly When TRUE the cookie will be made accessible only through the HTTP protocol.
	 */
	public static function Delete($Name, $Path = '/', $Domain = null, $Secure = false, $HttpOnly = false)
	{
		setcookie($Name, '', time() - 1, $Path, $Domain, $Secure, $HttpOnly);
	}
}

?>